#define _CRT_SECURE_NO_WARNINGS
#include "image_extractor.h"
#include "common.h"
#include "string.h"
#include "stdlib.h"

void test_get_position(FILE *fp) {
	int count = get_picture_count(fp);
	int i;
	for (i = 0; i < count; ++i) {
		long start_pos = get_start_position(fp, i + 1);
		long end_pos = get_end_position(fp, i + 1);
		printf("the %dth picture's start position is: %ld\n", i + 1, start_pos);
		printf("the %dth picture's end position is: %ld\n", i + 1, end_pos);
		printf("-------------------------------------------\n");
	}
}

void test_extract_all_file(FILE *fp, const char *out_dir) {
	if (fp == NULL) {
		return;
	}
	int i = 0;
	int count = get_picture_count(fp);
	for (; i < count; ++i) {
		char out_file[256];
		strcpy(out_file, out_dir);
		char buffer[128];
		sprintf(buffer, "/out-%d.png", i);
		strcat(out_file, buffer);
		FILE *out = fopen(out_file, "wb");
		if (out != NULL) {
			if (extract_file(fp, i + 1, out) == 0) {
				fclose(out);
			} else {
				fclose(out);
				remove(out_file);
			}
		}
	}
}

void test_get_picture_count(FILE *fp) {
	printf("There are %d pictures in src.rcc\n", get_picture_count(fp));
}

void test_copy_part(FILE *fp) {
	long start = get_start_position(fp, 1);
	long end = get_end_position(fp, 1);

	FILE *out = fopen("test.png", "wb");
	if (out != NULL) {
		copy_part(fp, (int) start, (int) end, out);
		fclose(out);
	}
}

int test_replace(FILE *fp, int sn, FILE *new_picture, FILE *out) {
	if (fp == NULL || new_picture == NULL || out == NULL) {
		return -1;
	}
	return replace(fp, sn, new_picture, out);
}

int test_image_extractor(int argc, char * argv[]) {
	FILE *fp;
	if (argc < 2) {
		printf("Warnning: More parameters needed.\n");
		printf("Help content:\n");
		printf(
				"	-extract:[extract] [input.rcc]'s [-all] inner-picture to the [out_dir] folder.\n");
		printf(
				"		ie. extract ./test/rcc/balloons.rcc -all ./test/out/balloons/\n");
		printf(
				"	-replace:[replace] [input.rcc]'s [sn]th resource with [*.png],output file is [out.rcc].\n");
		printf(
				"		ie. replace ./test/rcc/navcontrols.rcc 65 ./test/png/out-64.png ./test/out/navcontrols.rcc\n");
		return -1;
	} else if (argc == 5) {
		if (strcmp(argv[1], "extract") == 0) {
			if ((fp = fopen(argv[2], "rb")) != NULL) {
				if (strcmp(argv[3], "-all") == 0) {
					extract_all_file(fp, argv[4]);
				}
			}
		}
	} else if (argc == 6) {
		if (strcmp(argv[1], "replace") == 0) {
			if ((fp = fopen(argv[2], "rb")) != NULL) {
				FILE *new_picture = fopen(argv[4], "rb");
				FILE *out = fopen(argv[5], "wb");
				if (new_picture == NULL || out == NULL) {
					return -1;
				}
				int sn = atoi(argv[3]);
				test_replace(fp, sn, new_picture, out);
				fclose(new_picture);
				fclose(out);
				fclose(fp);
			}
		}
	}
}

void test_index_block() {
	FILE *fp;
	fp = fopen("test\\rcc\\balloons.rcc", "rb");
	int valid_length = get_valid_length(fp);
	printf("valid length is %d\n", valid_length);
	int count = get_picture_count(fp);
	Index **valid_array = (Index **) malloc(sizeof(Index *) * count);
	int i;
	for (i = 0; i < count; ++i) {
		valid_array[i] = (Index *) malloc(sizeof(Index));
	}
	get_index_block(fp, valid_array, count);

	sort_index_block_by_res_offset(valid_array, count);

	for (i = 0; i < count; ++i) {
		printf("%d ", valid_array[i]->res_offset);
	}
	printf("\n");

	sort_index_block_by_id(valid_array, count);

	for (i = 0; i < count; ++i) {
		printf("%d ", valid_array[i]->res_offset);
	}
	printf("\n");

	fclose(fp);
	for (i = 0; i < count; ++i) {
		free(valid_array[i]);
	}
	free(valid_array);
}

void check_binary() {
	FILE *fp1, *fp2;
	fp1 = fopen("test\\out\\balloons.rcc", "rb");
	fp2 = fopen("test\\rcc\\balloons.rcc", "rb");
	int checker1, checker2;
	fseek(fp1, 0, SEEK_END);
	long length = ftell(fp1);
	rewind(fp1);
	int i, flag = 1;
	for (i = 0; i < length; ++i) {
		fread((void*) (&checker1), 4, 1, fp1);
		fread(&checker2, 4, 1, fp2);
		if (memcmp(&checker1, &checker2, 1) != 0) {
			printf("diff line:%d\n", i / 16);
			flag = 0;
		}
	}
	if (flag) {
		printf("they are the same.\n");
	}

	fclose(fp1);
	fclose(fp2);
}

int main(int argc, char* argv[]) {
	test_image_extractor(argc, argv);
	//check_binary();
	//FILE *fp = fopen("test\\rcc\\balloons.rcc", "rb");
	//printf("%d\n", ftell(fp));
	//int one;
	//fread(&one, 4, 1, fp);
	//printf("%d\n", ftell(fp));
	//fclose(fp);
	return 0;
}
